import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
const Login = () => import('../views/Login.vue')
const Index = () => import('../views/Index.vue')
const Content = () => import('../views/Content.vue')
const Menu = () => import('../views/menu/Index.vue')
const Role = () => import('../views/role/Index.vue')
const User = () => import('../views/user/Index.vue')
const Category = () => import('../views/category/Index.vue')

const routes = [
  {
    path: '/',
    name: 'home',
    component: Content,
    children: [
      { path: '', component: Index, meta: { title: '图表' } },
      { path: 'menu', component: Menu, meta: { title: '菜单' } },
      { path: 'role', component: Role, meta: { title: '角色' } },
      { path: 'user', component: User, meta: { title: '管理员' } },
      { path: 'category', component: Category, meta: { title: '商品分类' } }
    ]
  },
  {
    path: '/login',
    component: Login,
    meta: {
      title: '登录'
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 全局前置守卫
router.beforeEach((to, from, next) => {
  // to 是要去的路由对象 from是正在离开的路由对象
  document.title = to.meta.title || '癔症';
  // 登录校验
  // if(localStorage)
  // next的作用是继续向后执行，将执行权交给后面的中间件
  next()
})

export default router
